package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @description: 权限Controller
 * @author: Kerwin
 * @date: 2020/3/8-11:57
 * @Version: 1.0.0
 */
@RequestMapping("/permission")
@RestController
public class PermissionController {


    @Reference
    private PermissionService permissionService;

    /**
     * 分页
     *
     * @param queryPageBean
     * @return com.itheima.entity.PageResult
     * @author Kerwin
     * @date 2020/3/8-15:50
     */
    @RequestMapping("/findPage")
    @PreAuthorize("hasAuthority('PERMISSION_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {

        System.out.println("------------");

        return permissionService.findPage(queryPageBean);

    }

    /**
     * 查询权限By Id
     *
     * @param id
     * @return com.itheima.entity.Result
     * @author Kerwin
     * @date 2020/3/8-15:50
     */
    @RequestMapping("/findById")
    @PreAuthorize("hasAuthority('PERMISSION_QUERY')")
    public Result findById(Integer id) {


        Permission permission = null;
        try {
            permission = permissionService.findById(id);
        } catch (Exception e) {
            e.printStackTrace();

            return Result.error("");
        }

        return Result.success("", permission);
    }

    /**
     * 编辑权限
     *
     * @param permission
     * @return com.itheima.entity.Result
     * @author Kerwin
     * @date 2020/3/8-15:49
     */
    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('PERMISSION_EDIT')")
    public Result edit(@RequestBody Permission permission) {
        Result result;

        try {
           result=  permissionService.edit(permission);

        } catch (Exception e) {
            e.printStackTrace();

            return Result.error("");
        }
        return result;
    }

    /**
     * 增加权限
     *
     * @param permission
     * @return com.itheima.entity.Result
     * @author Kerwin
     * @date 2020/3/8-15:49
     */
    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('PERMISSION_ADD')")
    public Result add(@RequestBody Permission permission) {

        Result result = null;

        try {
            result = permissionService.add(permission);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("后台错误，请联系管理员！！！");
        }

        return result;
    }

    /**
     * 删除权限
     *
     * @param id
     * @return com.itheima.entity.Result
     * @author Kerwin
     * @date 2020/3/8-16:00
     */
    @RequestMapping("/delete")
    @PreAuthorize("hasAuthority('PERMISSION_DELETE')")
    public Result delete(Integer id) {

        Result result = null;
        try {
            result = permissionService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("");
        }

        return result;
    }

    /**wq 3.8 找所有权限*/
    @RequestMapping("/findAll")
    public Result findAll(){
        Map<String, Object> map = permissionService.findAll();
        return Result.success("",map);//查询返回
    }
}
